package com.zhongxiangspace.controller.admin;


import com.zhongxiangspace.entity.FreemarkerHtmlConfiguration;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.VO.BannerVO;
import com.zhongxiangspace.entity.VO.TradeAreaVO;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 移动端首页生成
 */
@Controller
public class IndexMobileHtmlCreateController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private AdvertService advertService;

    /**
     * 生成静态页面
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/mobileIndex.do")
    public Result mobileIndex() {
        try {
            FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
            freemarkerHtmlConfiguration.setTemplateName("index.ftl");
            freemarkerHtmlConfiguration.setHtmlPath("/html/wap/index.html");
            freemarkerHtmlConfiguration.setTemplatePath("html/mobile");
            Map<String, Object> map = new HashMap<>();
            //banner
            QueryAnalyzer bannerQueryAnalyzer = new QueryAnalyzer();
            bannerQueryAnalyzer.setType("MOBILE");
            bannerQueryAnalyzer.setShowCode(1);
            List<BannerVO> list_banner = this.bannerService.queryBannerVO(bannerQueryAnalyzer);
            map.put("banner", list_banner);
            //热门商圈
            QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
            tradeAreaQueryAnalyzer.setShowCode(1);
            tradeAreaQueryAnalyzer.setHotCode(1);
            List<TradeAreaVO> tradeAreaVOS = this.countyService.queryTradeAreaVO(tradeAreaQueryAnalyzer);
            map.put("tradeArea", tradeAreaVOS);
            //查询所有 委托 投放 联合办公 数量
            Map<String, Object> sumMap = this.houseService.querySum();
            map.put("sumMap", sumMap);
            //不能错过的办公室
            QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
            buildingQueryAnalyzer.setShowCode(1);
            buildingQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
            map.put("officeBuildings", officeBuildingsList);
            //广告位
            QueryAnalyzer advertQueryAnalyzer = new QueryAnalyzer();
            advertQueryAnalyzer.setShowCode(1);
            List<Map<String, Object>> advertList = this.advertService.queryAdvert(advertQueryAnalyzer);
            map.put("advertList", advertList);
            //资讯文章1-选址百科
            QueryAnalyzer articleQueryAnalyzer = new QueryAnalyzer();
            articleQueryAnalyzer.setShowCode(1);
            articleQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> article_List = this.articleService.queryArticles(articleQueryAnalyzer);
            map.put("article_List", article_List);
            freemarkerHtmlConfiguration.setData(map);
         boolean flag= FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
         if(flag==true){
             return new NormalResult(true);
         }else {
             return new NormalResult(false);
         }
        } catch (Exception e) {
            e.printStackTrace();
            return new NormalResult(false);
        }
    }
}
